package com.sl.plugin.step.selenium.newtab;

import cn.hutool.core.collection.CollUtil;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import java.util.Set;

/**
 */
public class SeleniumNewTabComp extends AbstractProcessComp<WebDriver, WebDriver> implements ProcessCompService {


    public SeleniumNewTabComp(AbstractProcessCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @Override
    public WebDriver input(WebDriver driver) {
        String url = meta.getStrAttr("url");

        String time = "window.open(\"" + url + "\");";
        JavascriptExecutor js = (JavascriptExecutor) driver;

        js.executeScript(time);

        Set<String> handles = driver.getWindowHandles();
        driver.switchTo().window(CollUtil.get(handles, handles.size() - 1));
        return driver;
    }

}
